package com.ruoyi.web.controller.business;

import com.ruoyi.business.bo.HcxWithdrawApplyAddBo;
import com.ruoyi.business.bo.HcxWithdrawApplyEditBo;
import com.ruoyi.business.bo.HcxWithdrawApplyQueryBo;
import com.ruoyi.business.service.IHcxWithdrawApplyService;
import com.ruoyi.business.vo.HcxWithdrawApplyVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 提现申请Controller
 *
 * @author wsdz
 * @date 2022-08-26
 */
@Api(value = "提现申请控制器", tags = {"提现申请管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/WithdrawApply")
public class HcxWithdrawApplyController extends BaseController {

    private final IHcxWithdrawApplyService iHcxWithdrawApplyService;

    /**
     * 查询提现申请列表
     */
    @ApiOperation("查询提现申请列表")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxWithdrawApplyVo> list(@Validated HcxWithdrawApplyQueryBo bo) {
        return iHcxWithdrawApplyService.queryPageList(bo);
    }

    /**
     * 导出提现申请列表
     */
    @ApiOperation("导出提现申请列表")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:export')")
    @Log(title = "提现申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxWithdrawApplyQueryBo bo) {
        List<HcxWithdrawApplyVo> list = iHcxWithdrawApplyService.queryList(bo);
        ExcelUtil<HcxWithdrawApplyVo> util = new ExcelUtil<>(HcxWithdrawApplyVo.class);
        return util.exportExcel(list, "提现申请");
    }

    /**
     * 获取提现申请详细信息
     */
    @ApiOperation("获取提现申请详细信息")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxWithdrawApplyVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxWithdrawApplyService.queryById(id));
    }

    /**
     * 新增提现申请
     */
    @ApiOperation("新增提现申请")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:add')")
    @Log(title = "提现申请", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxWithdrawApplyAddBo bo) {
        return toAjax(iHcxWithdrawApplyService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改提现申请
     */
    @ApiOperation("修改提现申请")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:edit')")
    @Log(title = "提现申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxWithdrawApplyEditBo bo) {
        return toAjax(iHcxWithdrawApplyService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除提现申请
     */
    @ApiOperation("删除提现申请")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:remove')")
    @Log(title = "提现申请" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxWithdrawApplyService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入提现申请数据
     */
    @ApiOperation("导入提现申请数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:import')")
    @Log(title = "提现申请" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxWithdrawApplyVo> util = new ExcelUtil<>(HcxWithdrawApplyVo.class);
        List<HcxWithdrawApplyVo> hcxWithdrawApplyList;
        try {
            hcxWithdrawApplyList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxWithdrawApplyService.importHcxWithdrawApply(hcxWithdrawApplyList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxWithdrawApplyVo> util = new ExcelUtil<>(HcxWithdrawApplyVo.class);
        return util.importTemplateExcel("提现申请数据");
    }


    @ApiOperation("提现申请审核")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:audit')")
    @Log(title = "审核", businessType = BusinessType.AUDIT)
    @RepeatSubmit
    @PostMapping("/audit")
    public AjaxResult<Void> audit(@Validated @RequestBody HcxWithdrawApplyEditBo bo) throws Exception{
        Boolean audit = iHcxWithdrawApplyService.audit(bo);
        return toAjax(audit);
    }

    /**
     * 到账提现申请
     */
    @ApiOperation("提现申请到账")
    @PreAuthorize("@ss.hasPermi('business:WithdrawApply:daozhang')")
    @Log(title = "到账提现申请" , businessType = BusinessType.DAOZHANG)
    @PostMapping("/daozhang")
    public AjaxResult<Void> daozhang(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ids) {
        Boolean daozhang = iHcxWithdrawApplyService.daozhang(Arrays.asList(ids));
        return toAjax(daozhang);
    }
}
